<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtiframe.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtsurvey.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("components4phpfull/jtpagecontrol.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class standard_droplist2 extends Page
{
       public $JTFrameTrouble = null;
       public $JTJavaScript2 = null;
       public $JTDivFuelType = null;
       public $JTGroupBox3 = null;
       public $Label8 = null;
       public $JTJavaScript1 = null;
       public $Label7 = null;
       public $JTDivSetPrice = null;
       public $JTGroupBox2 = null;
       public $Image1 = null;
       public $Label6 = null;
       public $Label5 = null;
       public $Label4 = null;
       public $Label1 = null;
       public $JTGroupBox1 = null;
       public $JTDivInfo = null;
       public $imgInfo = null;
       public $imgNext = null;
       public $JTOpenSetPrice = null;
       public $hfGetCtr = null;
       public $imgBack = null;
       public $Image3 = null;
       public $JTGridModel = null;
       public $JTGridMnfr = null;
       public $editModel = null;
       public $editMnfr = null;
       public $hiddenModel = null;
       public $hiddenMnfr = null;
       public $btnDeleteModels = null;
       public $btnUpdateModels = null;
       public $btnAddModels = null;
       public $btnDelMnfr = null;
       public $btnUpdateMnfr = null;
       public $btnAddMnfr = null;
       public $btnCancel_std = null;
       public $btnDelete_std = null;
       public $btnUpdate_std = null;
       public $btnAdd_std = null;
       public $hiddenValueId = null;
       public $edValue = null;
       public $lblTextValue = null;
       public $hiddenCategory = null;
       public $hiddenTable = null;
       public $JTValues = null;
       public $JTDroplists = null;
       public $Label3 = null;
       public $Label2 = null;
       public $JTPageControl1 = null;
       public $JTSiteTheme1 = null;
       function imgInfoJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
        document.getElementById('JTDivInfo').Show();
       <?php

       }


       function checkExist($table,$value,$category)
       {
        if($category=='' || $category==null)
        $query_exist = "SELECT * FROM ".$table." WHERE company_id='".$_SESSION['company_id']."' AND value='".$value."'";

        if($category!='' && $category!=null && $category!='model')
        $query_exist = "SELECT * FROM ".$table." WHERE company_id='".$_SESSION['company_id']."' AND value='".$value."' AND category='".$category."'";

        if($category=='model')
        $query_exist = "SELECT * FROM ".$table." WHERE company_id='".$_SESSION['company_id']."' AND model_name='".$value."' AND mnfr_id='".$this->hiddenMnfr->getValue()."'";

        $result_exist = mysql_query($query_exist) or die("SELECT EXIST ".mysql_error());
        $exist = mysql_num_rows($result_exist);

        return $exist;
       }
       function imgNextJSClick($sender, $params)
       {

       ?>
       //var answer = confirm("Proceed to next step?");
	     //if(answer)
       //{
		    window.location.href = "set_personnel_list.php";
		   //}
       <?php

       }
       function imgNextJSMouseOut($sender, $params)
       {

       ?>
       document.images['imgNext'].src = 'image/btn_next.png';
       <?php

       }

       function imgNextJSMouseOver($sender, $params)
       {

       ?>
       document.images['imgNext'].src = 'image/btn_next2.png';
       <?php

       }

       function imgBackJSClick($sender, $params)
       {

       ?>
       window.location.href = "set_register_company_edit.php";

       <?php

       }

       function standard_droplist2JSLoad($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDroplists_colitem_hdr_0').width = '116';
       document.getElementById('JTDroplists_colitem_hdr_1').width = '250';

       document.getElementById('JTDroplists_colitem_body_0').width = '116';
       document.getElementById('JTDroplists_colitem_body_1').width = '250';
       <?php

       }



       function JTGridModelShow($sender, $params)
       {
       $mnfr_id = $this->hiddenMnfr->getValue();
       $model_id = $this->hiddenModel->getValue();
       $arr_model = array();
       $result_model = mysql_query("SELECT * FROM lookup_models_tbl WHERE company_id='".$_SESSION['company_id']."' AND mnfr_id='".$mnfr_id."' order by model_name ASC") or die("SELECT MODEL".mysql_error());
       while($row_model = mysql_fetch_array($result_model))
       {
         $model_id = $row_model['id'];
         $get_model_id = $this->hiddenModel->getValue();

         $model_value = $row_model['model_name'];
         if($model_id==$get_model_id)
         {
         $model_link = "<a style='background-color:yellow' href=set_standard_droplist.php?layer=1&company_id=".$_SESSION['company_id']."&mnfr_id=".$mnfr_id."&model_id=".$model_id.">".$model_value.
         "</a>";
         }
         if($model_id!=$get_model_id)
         {
         $model_link = "<a href=set_standard_droplist.php?layer=1&company_id=".$_SESSION['company_id']."&mnfr_id=".$mnfr_id."&model_id=".$model_id.">".$model_value.
         "</a>";
         }
         $arr_model[] = array('Model' => $model_link);
       }
         $this->JTGridModel->setCellData($arr_model);


       }

       function JTGridMnfrShow($sender, $params)
       {
       $arr_mnfr = array();
       $result_mnfr = mysql_query("SELECT * FROM lookup_vehicle_tbl WHERE company_id='".$_SESSION['company_id']."' AND category='manufacturer' order by value asc") or die("SELECT MNFR".mysql_error());
       while($row_mnfr = mysql_fetch_array($result_mnfr))
       {
         $mnfr_id = $row_mnfr['id'];
         $get_mnfr_id = $this->hiddenMnfr->getValue();
         $mnfr_value = $row_mnfr['value'];
         if($mnfr_id==$get_mnfr_id)
         {
         $mnfr_link = "<a style='background-color:yellow' href=set_standard_droplist.php?layer=1&company_id=".$_SESSION['company_id']."&mnfr_id=".$mnfr_id.">".$mnfr_value.
         "</a>";
         }
         if($mnfr_id!=$get_mnfr_id)
         {
         $mnfr_link = "<a href=set_standard_droplist.php?layer=1&company_id=".$_SESSION['company_id']."&mnfr_id=".$mnfr_id.">".$mnfr_value.
         "</a>";
         }
         $arr_mnfr[] = array('Manufacturer' => $mnfr_link);
       }
         $this->JTGridMnfr->setCellData($arr_mnfr);


       }


        function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back1.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back21.png"
       <?php

       }



       function btnModelDeleteClick($sender, $params)
       {
       $mnfr_id = $this->hiddenMnfr->getValue();
        $model_id = $this->hiddenModel->getValue();
        mysql_query("DELETE FROM lookup_models_tbl WHERE company_id='".$_SESSION['company_id']."' AND id='".$model_id."'");

        redirect('set_standard_droplist.php?layer=1&company_id='.$_SESSION['company_id'].'&mnfr_id='.$mnfr_id);


       }

       function btnModelUpdateClick($sender, $params)
       {
        if($this->editModel->Text=='' || $this->editModel->Text==null)
        redirect('set_standard_droplist.php?layer=1');

        $exist = $this->checkExist('lookup_models_tbl',$this->editModel->getText(),'model');
        if($exist==0)
        {
        $mnfr_id = $this->hiddenMnfr->getValue();
        $model_id = $this->hiddenModel->getValue();
        mysql_query("UPDATE lookup_models_tbl SET model_name='".$this->editModel->getText()."'
        WHERE company_id='".$_SESSION['company_id']."' AND id='".$model_id."'") or
        die("UPDATE MODEL".mysql_error());
        }
        redirect('set_standard_droplist.php?layer=1&company_id='.$_SESSION['company_id'].'&mnfr_id='.$mnfr_id.'&model_id='.$model_id);


       }

       function btnModelAddClick($sender, $params)
       {
        if($this->editModel->Text=='' || $this->editModel->Text==null)
        redirect('set_standard_droplist.php?layer=1');

        $exist = $this->checkExist('lookup_models_tbl',$this->editModel->getText(),'model');
        if($exist==0)
        {
        $mnfr_id = $this->hiddenMnfr->getValue();
        mysql_query("INSERT INTO lookup_models_tbl (company_id, mnfr_id, model_name)
        VALUES ('".$_SESSION['company_id']."',
        '".$mnfr_id."','".$this->editModel->getText()."')") or
        die("INSERT MODEL".mysql_error());
        }
        redirect('set_standard_droplist.php?layer=1&company_id='.$_SESSION['company_id'].'&mnfr_id='.$mnfr_id);


       }

       function btnMnfrDeleteClick($sender, $params)
       {
        $mnfr_id = $this->hiddenMnfr->getValue();
        mysql_query("DELETE FROM lookup_vehicle_tbl WHERE company_id='".$_SESSION['company_id']."' AND id='".$mnfr_id."'");

        redirect('set_standard_droplist.php?layer=1&company_id='.$_SESSION['company_id'].'&mnfr_id='.$mnfr_id);


       }

       function btnMnfrUpdateClick($sender, $params)
       {
        if($this->editMnfr->Text=='' || $this->editMnfr->Text==null)
        redirect('set_standard_droplist.php?layer=1');

        $exist = $this->checkExist('lookup_vehicle_tbl',$this->editMnfr->getText(),'manufacturer');
        if($exist==0)
        {
        $mnfr_id = $this->hiddenMnfr->getValue();
        mysql_query("UPDATE lookup_vehicle_tbl SET value='".$this->editMnfr->getText()."'
        WHERE company_id='".$_SESSION['company_id']."' AND id='".$mnfr_id."'");
        }
        redirect('set_standard_droplist.php?layer=1&company_id='.$_SESSION['company_id'].'&mnfr_id='.$mnfr_id);


       }

       function btnMnfrAddClick($sender, $params)
       {
       if($this->editMnfr->Text=='' || $this->editMnfr->Text==null)
       redirect('set_standard_droplist.php?layer=1');

       $exist = $this->checkExist('lookup_vehicle_tbl',$this->editMnfr->getText(),'manufacturer');
       if($exist==0)
       {
       mysql_query("INSERT INTO lookup_vehicle_tbl
       (company_id, category, value)
       VALUES ('".$_SESSION['company_id']."',
       'manufacturer','".$this->editMnfr->getText()."')") or
       die("INSERT MNFR".mysql_error());
       }

       redirect('set_standard_droplist.php?layer=1&company_id='.$_SESSION['company_id'].'&mfr_id='.$mnfr_id);


       }

       function btnCancel_stdClick($sender, $params)
       {
       redirect('vehicle_list.php?company_id='.$_SESSION['company_id']);


       }

       function btnDelete_stdClick($sender, $params)
       {
       $id = $this->hiddenValueId->getValue();
       $table = $this->hiddenTable->getValue();
       $category = $this->hiddenCategory->getValue();

       $query_delete = "DELETE FROM ".$table." WHERE company_id='".$_SESSION['company_id']."' AND id='".$id."'";
       mysql_query($query_delete);
       redirect('set_standard_droplist.php?save_to='.$table.'&category='.$category.'&company_id='.$_SESSION['company_id']);


       }

       function btnUpdate_stdClick($sender, $params)
       {
       $id = $this->hiddenValueId->getValue();
       $table = $this->hiddenTable->getValue();
       $category = $this->hiddenCategory->getValue();

       if($this->edValue->Text=='' || $this->edValue->Text==null)
       return false;

       $exist = $this->checkExist($table,$this->edValue->Text,null);

       if($exist==0)
       {
       $query_update = "UPDATE ".$table." SET
       value = '".$this->edValue->getText()."'
       WHERE id='".$id."'";
       mysql_query($query_update) or die(mysql_error());
       }
       redirect('set_standard_droplist.php?save_to='.$table.'&category='.$category.'&company_id='.$_SESSION['company_id']);
       }

       function btnAdd_stdClick($sender, $params)
       {
       $id = $this->hiddenValueId->getValue();
       $table = $this->hiddenTable->getValue();
       $category = $this->hiddenCategory->getValue();

       if($this->edValue->Text=='' || $this->edValue->Text==null)
       return false;

       $exist = $this->checkExist($table,$this->edValue->Text,null);

       if($exist==0)
       {
       $result_values = mysql_query("INSERT INTO ".$table."(company_id, value,category) VALUES (
       '".$_SESSION['company_id']."',
       '".$this->edValue->getText()."',
       '".$category."'
       )")or die("INSERT VALUE - " . mysql_error());
       }
       redirect('set_standard_droplist.php?save_to='.$table.'&category='.$category.'&id='.$id.'&company_id='.$_SESSION['company_id']);


       }

       function standard_droplist2Create($sender, $params)
       {

       $table = null;
       $category = null;
       $id = null;
       $this->hiddenValueId->setValue($_GET['id']);
       $this->hiddenTable->setValue($_GET['save_to']);
       $this->hiddenCategory->setValue($_GET['category']);
       $table = $this->hiddenTable->getValue();
       $category = $this->hiddenCategory->getValue();

       $get_ctr = $_GET['get_ctr'];
       if(($get_ctr!='' || $get_ctr!=null) && isset($get_ctr))
       $this->hfGetCtr->setValue($get_ctr);

       $this->JTPageControl1->TabIndex = $_GET['layer'];

       $this->JTFrameTrouble->setURL('set_trouble_remedy_droplist.php');

       //$this->imgBack->setLink('vehicle_id?company_id='.$_SESSION['company_id']);

       $label_array = array('Fuel', '',
       'Tire', '', '', '',
       'Vehicle', '',
       'Account', 'Expense', '',
       'Inventory', '', '', '',
       'Personnel','',
       'Reminder',
       'Work Summary','');

       /*$fields_array = array('Brand', 'Type',
       'Manufacturer', 'Size', 'Type', 'Wheel Size',
       'Incident Type', 'Type/Style',
       'Account Title', 'Description', 'Type',
       'Manufacturer', 'Storeroom/Location', 'Type', 'Unit of Measure',
       'Incident Type', 'Job Title', 'Department',
       'Type',
       'Type', 'Serviced By', 'Labor Description');*/

       $fields_array = array('Brand', 'Type',
       'Manufacturer', 'Size', 'Type', 'Wheel Size',
       'Incident Type', 'Type/Style',
       'Account Title', 'Description', 'Type',
       'Manufacturer', 'Storeroom/Location', 'Type', 'Unit of Measure',
       'Job Title', 'Department',
       'Type',
       'Type','Labor Description');

       $category_array = array('brands', 'octane/types',
       'manufacturers', 'sizes', 'types', 'wheel sizes',
       'journal categories', 'body styles',
       'accounts', 'descriptions', 'types',
       'manufacturers', 'storeroom/locations', 'types', 'unit of measure',
       'job titles', 'departments',
       'type',
       'descriptions','task descriptions');

       $tbl_array = array('lookup_fuel_tbl', 'lookup_fuel_tbl',
       'lookup_tire_tbl', 'lookup_tire_tbl', 'lookup_tire_tbl', 'lookup_tire_tbl',
       'lookup_vehicle_tbl', 'lookup_vehicle_tbl',
       'lookup_expense_tbl', 'lookup_expense_tbl', 'lookup_expense_tbl',
       'lookup_inventory_tbl', 'lookup_inventory_tbl', 'lookup_inventory_tbl', 'lookup_inventory_tbl',
       'lookup_personnel_tbl', 'lookup_personnel_tbl',
       'lookup_reminder_tbl',
       'lookup_workorder_tbl','lookup_workorder_tbl');

       $arr = array();
       $ctr = 0;
       $count = count($fields_array);
       while($ctr<=$count)
       {
        if($ctr==$this->hfGetCtr->getValue() && is_numeric($this->hfGetCtr->getValue()))
        {
         $tools = "<a href='set_standard_droplist.php?save_to=".$tbl_array[$ctr]."&category=".$category_array[$ctr]."&company_id=".$_SESSION['company_id']."&get_ctr=".$ctr."' style='background-color:yellow'>".$fields_array[$ctr]."</a>";
         if($category_array[$ctr]=='octane/types')
          {
           $tools = "<a href='set_standard_droplist.php?save_to=".$tbl_array[$ctr]."&category=".$category_array[$ctr]."&company_id=".$_SESSION['company_id']."&get_ctr=".$ctr."' style='background-color:yellow'>".$fields_array[$ctr]."</a>&nbsp;<img src='image/info.png' style='cursor:pointer' onclick='showFuel()' title='More Info...'/>";
          }
        }
        else//if($ctr!=$this->hfGetCtr->getValue())
        {
        $tools = "<a href='set_standard_droplist.php?save_to=".$tbl_array[$ctr]."&category=".$category_array[$ctr]."&company_id=".$_SESSION['company_id']."&get_ctr=".$ctr."'>".$fields_array[$ctr]."</a>";
          if($category_array[$ctr]=='octane/types')
          {
           $tools = "<a href='set_standard_droplist.php?save_to=".$tbl_array[$ctr]."&category=".$category_array[$ctr]."&company_id=".$_SESSION['company_id']."&get_ctr=".$ctr."'>".$fields_array[$ctr]."</a>&nbsp;<img src='image/info.png' style='cursor:pointer' onclick='showFuel()' title='More Info...'/>";
          }
        }
        $arr[] = array('Category ' => $label_array[$ctr],
        'Droplist Fields ' => $tools);
        $ctr++;
       }
       $this->JTDroplists->setCellData($arr);

       if($table!=null)
       {
       $ctr = 0;
       $arr2 = array();
       $query2 = "SELECT * FROM ".$table." WHERE company_id='".$_SESSION['company_id']."' AND category='".$category."' ORDER BY value ASC";
       //var_dump($query2);
       $result_values = mysql_query($query2);
       while($row_values = mysql_fetch_array($result_values))
       {
        $id = $row_values['id'];
        $get_id = $this->hiddenValueId->getValue();
        if($id==$get_id)
        {
        $value = "<a style='background-color:yellow' href='set_standard_droplist.php?save_to=".$table."&id=".$id."&category=".$category."&company_id=".$_SESSION['company_id']."'>".$row_values['value']."</a>";
        }
        if($id!=$get_id)
        {
        $value = "<a href='set_standard_droplist.php?save_to=".$table."&id=".$id."&category=".$category."&company_id=".$_SESSION['company_id']."'>".$row_values['value']."</a>";
        }
        if($table!='lookup_fuel_tbl' || $category=='octane/types')
        $arr2[] = array('Values'=>$value);
        if($table=='lookup_fuel_tbl' && $category=='brands')
        {
         $price = "<span style='text-decoration:underline;color:blue;cursor:pointer' onclick=openSetPrice('".$id."','".base64_encode($row_values['value'])."')>Set Price</span>
         <img onclick=showSetPrice(); src='image/info.png' title='More Info...' style='cursor:pointer' />";
         $arr2[] = array('Values'=>$value,
         ''=>$price);
        }
        $ctr++;

       $query_select_value = "SELECT * FROM  ".$table." WHERE company_id='".$_SESSION['company_id']."' AND id='".$this->hiddenValueId->getValue()."'";
       $result_select_value = mysql_query($query_select_value) or die(mysql_error());
       $row_select_value = mysql_fetch_array($result_select_value);
       $this->edValue->setText($row_select_value['value']);
       }
       $this->JTValues->setCellData($arr2);
       }

       $get_mnfr = $_GET['mnfr_id'];
       if(($get_mnfr!='' || $get_mnfr!=null) && isset($get_mnfr))
       $this->hiddenMnfr->setValue($get_mnfr);

       $this->hiddenModel->setValue($_GET['model_id']);
       $mnfr_id = $this->hiddenMnfr->getValue();
       $model_id = $this->hiddenModel->getValue();


       //DELETE MNFR(CHECK IF MNFR STILL EXIST FOR MODEL)
        $query_check_mnfr = "SELECT id FROM lookup_vehicle_tbl where id='".$mnfr_id."'";
        $result_check_mnfr = mysql_query($query_check_mnfr) or die("SELECT CHECK MNFR VALUE ".mysql_error());
        $count_check_mnfr = mysql_num_rows($result_check_mnfr);
        if($count_check_mnfr==0)
        {
        $this->hiddenMnfr->setValue('');
        }

       //$this->imgBack->setLink('vehicle_list.php?company_id='.$_SESSION['company_id']);

       $result_mnfr = mysql_query("SELECT * FROM lookup_vehicle_tbl WHERE company_id='".$_SESSION['company_id']."' AND id='".$mnfr_id."'") or
       die("SELECT MNFR".mysql_error());
       $row_mnfr = mysql_fetch_array($result_mnfr);

       $result_model = mysql_query("SELECT * FROM lookup_models_tbl WHERE company_id='".$_SESSION['company_id']."' AND id='".$model_id."' order by model_name ASC") or
       die("SELECT MODEL".mysql_error());
       $row_model = mysql_fetch_array($result_model);

       $this->editMnfr->setText($row_mnfr['value']);
       $this->editModel->setText($row_model['model_name']);

       }



}

global $application;

global $standard_droplist2;

//Creates the form
$standard_droplist2=new standard_droplist2($application);

//Read from resource file
$standard_droplist2->loadResource(__FILE__);

//Shows the form
$standard_droplist2->show();

?>